package com.bj.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.bj.model.auto.ChannelInfos;
import com.bj.service.ChannelService;
import com.github.pagehelper.PageInfo;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

@Controller
@ResponseBody
@RequestMapping("/channelInfos")
public class ChannelInfosController {
    @Autowired
    private ChannelService channelService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    private static Logger logger = LoggerFactory.getLogger(ChannelInfosController.class);

    @RequestMapping("/getByid")
    public ChannelInfos findChannelInfosById(Long id){
        kafkaTemplate.send("topic1","key","data");
        return channelService.findChannelInfosById(id);
    }

    @RequestMapping("/getAllByPage")
    public PageInfo findAllChannelInfosByPage(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "10")int pageSize){
        PageInfo pageInfo = channelService.findAllChannelInfosByPage(pageNum, pageSize);
        return pageInfo;
    }

    @RequestMapping(value = "/testRequestBody",method = RequestMethod.POST)
    public void testRequestBody(@RequestBody ChannelInfos info){
        System.out.println(info);
    }

    @RequestMapping(value = "/testNoContentType",method = RequestMethod.POST)
    public void testNoContentType(HttpServletRequest request) throws IOException {
        System.out.println("request.getContentType:"+request.getContentType());
        if(request.getContentType().startsWith("multipart/form-data")|| request.getContentType().equals("application/x-www-form-urlencoded") ){
           // System.out.println(JSONUtils.toJSONString(request.getParameterMap()));
            Enumeration<String> parameterNames = request.getParameterNames();
            while (parameterNames.hasMoreElements()){
                System.out.println(parameterNames.nextElement());
            }
            System.out.println(request.getParameterMap().size());
        }
        BufferedReader reader = request.getReader();
        String str,wholeString ="";
        while ((str = reader.readLine()) != null){
            wholeString += str;
        }
        System.out.println(wholeString);
        System.out.println("request.getParameterMap:"+JSONUtils.toJSONString(request.getParameterMap()));
        System.out.println(request.getContentLength());
        Enumeration<String> headers = request.getHeaders("content-type");
        String header = request.getHeader("content-type");
        System.out.println(headers.nextElement());
        System.out.println("header:"+header);
        System.out.println("test no content type");
    }

    @KafkaListener(topics = {"topic1"})
    public void kafkaListener(ConsumerRecord<?, ?> cr){
        logger.info("{} - {} : {}", cr.topic(), cr.key(), cr.value());
    }
}
